package com.xyz.controller;

import com.xyz.aop.Param;
import com.xyz.service.UserService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("system")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping("passwordLessLogin")
    public DataResult passwordLessLogin(
            @Param String username,
            @Param String clientId,
            @Param String ts,
            @Param String signature
    ) {
        try {
            return userService.passwordLessLogin(username, clientId, ts, signature);
        } catch (Exception e) {
            logger.error("passwordLessLogin", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("login")
    public DataResult login(
            @Param String username,
            @Param String password
    ) {
        try {
            return userService.login(username, password);
        } catch (Exception e) {
            logger.error("login", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("appLogin")
    public DataResult appLogin(
            @Param String username,
            @Param String password
    ) {
        try {
            return userService.appLogin(username, password);
        } catch (Exception e) {
            logger.error("appLogin", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("WeChatLogin")
    public DataResult WeChatLogin(
            @Param String phoneNumber,
            @Param String password
    ) {
        try {
            return userService.WeChatLogin(phoneNumber, password);
        } catch (Exception e) {
            logger.error("WeChatLogin", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("logout")
    public DataResult logout(
            LoginInfo li
    ) {
        try {
            return userService.logout(li);
        } catch (Exception e) {
            logger.error("logout", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectUser")
    public DataResult selectUser(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false) String username,
            @Param(isRequired = false) String employeeName
    ) {
        try {
            return userService.selectUser(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), username, employeeName);
        } catch (Exception e) {
            logger.error("selectUser", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertUser")
    public DataResult insertUser(
            LoginInfo li,
            @Param String username,
            @Param String password
    ) {
        try {
            return userService.insertUser(li, username, password);
        } catch (Exception e) {
            logger.error("insertUser", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateUser")
    public DataResult updateUser(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String userId,
            @Param String password
    ) {
        try {
            return userService.updateUser(li, Long.parseLong(userId), password);
        } catch (Exception e) {
            logger.error("updateUser", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteUser")
    public DataResult deleteUser(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String userId
    ) {
        try {
            return userService.deleteUser(li, Long.parseLong(userId));
        } catch (Exception e) {
            logger.error("deleteUser", e);
            return DataResult.build9500();
        }
    }

}
